package com.kfm.jdbc.day0327;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo04 {

    public static void main(String[] args) {
        Class<Student> studentClass = Student.class;

        int modifiers = studentClass.getModifiers();

        System.out.println(modifiers);
        System.out.println(Modifier.toString(modifiers));

        // public 内部类
        Class<?>[] classes = studentClass.getClasses();
//        System.out.println(Arrays.toString(classes));

        // 父类
        Class<? super Student> superclass = studentClass.getSuperclass();
//        System.out.println(superclass);

        // 父接口
        Class<?>[] interfaces = studentClass.getInterfaces();
//        System.out.println(Arrays.toString(interfaces));

        // 构造
        // 获取所有 public 修饰的构造
        Constructor<?>[] constructors = studentClass.getConstructors();
//        System.out.println(Arrays.toString(constructors));
        // 所有声明的构造
        Constructor<?>[] declaredConstructors = studentClass.getDeclaredConstructors();
//        System.out.println(Arrays.toString(declaredConstructors));

        try {
            // Student(int)
            Constructor<Student> constructor = studentClass.getDeclaredConstructor(int.class);
            Constructor<Student> constructor1 = studentClass.getDeclaredConstructor(Integer.class);
//            System.out.println(constructor);
//            System.out.println(constructor1);

            // 创建对象
//            Student student = constructor.newInstance(6);
//            // 通过 class 对象创建对象 【不推荐】
//            Student student1 = studentClass.newInstance();
//            System.out.println(student1);

            Constructor<Student> declaredConstructor = studentClass.getDeclaredConstructor(double.class);

            // 修改访问限制
            declaredConstructor.setAccessible(true);

            Student student = declaredConstructor.newInstance(1.1);
            System.out.println(student);

        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }
}



